package com.gy.education.mine;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.gy.education.BaseActivity;
import com.gy.education.MainActivity;
import com.gy.education.R;
import com.gy.education.utils.AnimUtil;
import com.gy.education.utils.SPHelper;

/**
 * 作者：gy on 16/7/12
 * 说明：登录
 */
public class LoginActivity extends BaseActivity {

    private TextView tvForger, tvRegister,tvCode,btnLogin;
    private EditText etPhone, etPwd;
    private ImageView imgLogo;
    private Intent intent;
    private String strPhone, strPwd;
    private int fromMainFlag;
    private int time = 60;

    private BroadcastReceiver receiver = new BroadcastReceiver() {

        @Override
        public void onReceive(Context context, Intent intent) {
            // TODO Auto-generated method stub
            if ("close_login".equals(intent.getAction())) {
                LoginActivity.this.finish();
            }
        }
    };

    @Override
    protected void initView() {
        setContentViewRes(R.layout.activity_login);
        _baseBack.setVisibility(View.INVISIBLE);
        setTitleMsg("登录");
        tvForger = (TextView) findViewById(R.id.activity_login_tv_forget);
        tvRegister = (TextView) findViewById(R.id.activity_login_tv_register);
        tvCode = (TextView) findViewById(R.id.activity_login_tv_code);
        btnLogin = (TextView) findViewById(R.id.activity_login_btn_login);
        etPhone = (EditText) findViewById(R.id.activity_login_et_phone);
        etPwd = (EditText) findViewById(R.id.activity_login_et_pwd);
        imgLogo = (ImageView) findViewById(R.id.activity_login_img_logo);
    }

    @Override
    protected void initData() {
        fromMainFlag = getIntent().getIntExtra("fromMainFlag", 0);
        IntentFilter filter = new IntentFilter();
        filter.addAction("close_login");
        registerReceiver(receiver, filter);
    }

    @Override
    protected void setListener() {
        super.setListener();
        tvForger.setOnClickListener(this);
        tvRegister.setOnClickListener(this);
        btnLogin.setOnClickListener(this);
        tvCode.setOnClickListener(this);
    }

    @Override
    protected void fillData() {
        super.fillData();
        if (!TextUtils
                .isEmpty(SPHelper.make(this).getStringData("phone", null))) {
            etPhone.setText(SPHelper.make(this).getStringData("phone", null));
        }
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            case R.id.activity_login_tv_forget:// 忘记密码

                break;
            case R.id.activity_login_tv_register:// 注册
                intent = new Intent(this, RegisterActivity.class);
                startActivityForResult(intent,101);
                AnimUtil.pushLeftInAndOut(this);
                break;
            case R.id.activity_login_tv_code://获取验证码
                if("获取验证码".equals(tvCode.getText().toString())){
                    timeHandler.sendEmptyMessageDelayed(0,0);
                }
                break;
            case R.id.activity_login_btn_login:// 登录
                intent = new Intent(this, MainActivity.class);
                startActivity(intent);

                strPhone = etPhone.getText().toString().trim();
                strPwd = etPwd.getText().toString().trim();
                if (TextUtils.isEmpty(strPhone)) {
                    showToast("请输入手机号");
                } else if (strPhone.length() != 11) {
                    showToast("请输入正确的手机号");
                } else if (TextUtils.isEmpty(strPwd)) {
                    showToast("请输入密码");
                } else if (strPwd.length() != 6) {
                    showToast("密码长度为6位");
                } else {

                }
                break;
            default:
                break;
        }
    }

    private Handler timeHandler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if(time > 0){
                time--;
                tvCode.setText(time+"秒后可重新获取");
                timeHandler.sendEmptyMessageDelayed(0,1000);
            } else {
                time = 60;
                tvCode.setText("获取验证码");
            }
        }
    };

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver(receiver);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(resultCode == 101){
            this.finish();
        }
    }
}
